package com.kancy.stream.rabbit.config;

import com.kancy.stream.rabbit.converter.JacksonMessageConverter;
import com.kancy.stream.rabbit.converter.MessageConverter;
import com.kancy.stream.rabbit.handler.ExceptionHandler;
import com.kancy.stream.rabbit.handler.LogExceptionHandler;
import com.kancy.stream.rabbit.handler.LogMessageHandler;
import com.kancy.stream.rabbit.handler.MessageHandler;
import com.kancy.stream.rabbit.properties.RabbitMessageProperties;
import com.kancy.stream.rabbit.service.MessageChannelService;
import com.kancy.stream.rabbit.service.impl.MessageChannelServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
 * RabbitMessageChannelAutoConfiguration
 *
 * @author kancy
 * @date 2020/3/7 21:10
 */
@Import({RabbitMessageChannelRegistrar.class, RabbitMessageChannelListenerRegistrar.class})
public class RabbitMessageChannelAutoConfiguration {

    @Bean
    @ConfigurationProperties("stream.rabbit")
    public RabbitMessageProperties streamProperties(){
        return new RabbitMessageProperties();
    }

    @Bean
    @ConditionalOnMissingBean
    public DynamicInterfaceBeanResolver dynamicInterfaceBeanResolver(){
        return new DynamicInterfaceBeanResolver();
    }

    @Bean
    @ConditionalOnMissingBean
    public MessageConverter messageConverter(){
        return new JacksonMessageConverter();
    }

    @Bean
    @ConditionalOnMissingBean
    public MessageHandler logMessageHandler(){
        return new LogMessageHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public ExceptionHandler logExceptionHandler(){
        return new LogExceptionHandler();
    }

    @Bean
    @ConditionalOnMissingBean
    public MessageChannelService messageChannelService(RabbitTemplate rabbitTemplate, RabbitMessageProperties messageProperties,
                                                       DynamicInterfaceBeanResolver dynamicInterfaceBeanResolver){
        return new MessageChannelServiceImpl(rabbitTemplate,messageProperties, dynamicInterfaceBeanResolver);
    }
}
